/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package knockknock.app;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import knockknock.In;
import knockknock.KnockknockProtocol;
import de.thwildau.tm.moses.abnf.io.Transmitable;

/**
 * Server part of the socket-based KnockKnock communication
 */
public class KnockKnockServerApp {

    public static final int PORT = 4444;


    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT);
            System.out.println("Listening on port " + PORT + " ...");
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + PORT);
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        final DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
        DataInputStream in = new DataInputStream(clientSocket.getInputStream());
        KnockknockProtocol kkp = new KnockknockProtocol();
        kkp.add(new KnockknockProtocol.ResultListener() {
			
			@Override
			public void onResult(Transmitable result) {
				try {
					result.write(out);
					out.flush();
				} catch(IOException e) {
					System.err.println("Unexpected error processing " + result);
					e.printStackTrace();
				}
			}
		});

        kkp.processEvent(null);
        
        while (clientSocket.isBound()) {
        	In input = new In();
        	input.read(in);
        	System.out.println("Processing Event " + input);
        	kkp.processEvent(input);
		}
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}
